package net.flyinggroup.introduction;

import io.rsocket.RSocket;
import io.rsocket.core.RSocketConnector;
import io.rsocket.transport.netty.client.TcpClientTransport;
import io.rsocket.util.DefaultPayload;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;

import java.time.Duration;

import static net.flyinggroup.introduction.Constants.TCP_PORT;

@Slf4j
public class ChannelClient {
    private final RSocket socket;
    private final GameController gameController;

    public ChannelClient() {
        socket = RSocketConnector.connectWith(TcpClientTransport.create(TCP_PORT)).block();
        this.gameController = new GameController("Client Player");
    }

    public void playGame() {
        socket.requestChannel(
                Flux.interval(Duration.ofSeconds(2))
                        .map(l-> DefaultPayload.create(String.valueOf(l))).take(5))
                .doOnNext(payload -> {
                    log.info("Channel request from server: {}", payload.getDataUtf8());
                })
                .blockLast();
    }
    public void dispose() {
        socket.dispose();
    }
}
